import Mustache from 'mustache'
import { getMustacheData } from './handlers'

export const methodsVue = async (
    apiItems: IApiItem[],
    useDefaultValuesInJs?: boolean,
) => {
    const data = await getMustacheData(apiItems)

    return Mustache.render(
        `import {
	{{#data}}
	{{methodLower}}{{funcName}}Api,
	{{/data}}
} from '@/repository'

export default {
	data() {
	    return {
			{{#data}}
			// {{title}}
			{{funcNameCamel}}: {{#useDefaultValuesInJs}} {{{initialExampleResValue}}} {{/useDefaultValuesInJs}}{{^useDefaultValuesInJs}} null {{/useDefaultValuesInJs}},
			{{/data}}
		}
	},
	methods: {
		{{#data}}
		// {{title}}
		async fetch{{funcName}}({{{funcParamsJs}}}) {
			const { code, data, msg } = await {{methodLower}}{{funcName}}Api({{{funcParamsJs}}})
			if (code !== 0) {
				this.$toast(msg)
				return
			}
			this.{{funcNameCamel}} = data
		},
		{{/data}}
	}
}
`,
        { data, useDefaultValuesInJs },
    )
}
